var Global = require("../util/global.js");
var websocket = require("../util/websocket.js");
var message = require("../util/message.js");

cc.Class({
    extends: cc.Component,

    properties: {

        i:cc.Sprite,
        u:cc.Sprite,
        
        alert:cc.Node,
        timeLabel:cc.Label,
        windLabel:cc.Label,

        wind:0,
        time:60,
        touchFlag:false,
        updateIndex:0,
    },

    onLoad () {
        //关闭调试信息
        cc.debug.setDisplayStats(false);

        //关闭右下角vconsole
        if(window.vConsole)window.vConsole.$dom.style.display = 'none';

        //开启物理引擎
        cc.director.getPhysicsManager().enabled = true;
        
        //开启碰撞检测
        var manage = cc.director.getCollisionManager();
        manage.enabled = true;
        //manage.enabledDebugDraw = true;

        //初始化
        this.init();
    },

    start () {
        
    },
    init(){
        cc.log("初始化");

        //隐藏弹出框
        this.alert.active = false;

        //初始化websocket
        this.initWebSocket();

        //初始化房间
        this.initRoom();

        //初始化时间
        this.timeStart();

        //可以操作
        this.touchFlag = true;
    },
    initWebSocket(){
        if(Global.socket){
            Global.socket.sock.parent = this;
            Global.socket.sock.onmessage = this.onMessage;
        }
    },
    initRoom(){
        //设置房间时间
        this.time = Global.room.time;
        this.timeLabel.node.getComponent(cc.Label).string = this.time;

    },
    chooseShoe(){
        cc.log("选择鞋子");
    },
    shoeBothEmpty(){
        cc.log("双方是不是都没鞋子了");

        if(this.i.getComponent('i').shoeList.childrenCount==0
            &&this.i.getComponent('i').weapon==null
            &&this.u.getComponent('u').shoeList.childrenCount==0
            &&this.u.getComponent('u').weapon==null){
            this.winOrLose();
        }
    },
    winOrLose(){
        cc.log("输赢判定");
        
        var iblood = this.i.getComponent('i').blood;
        var ublood = this.u.getComponent('u').blood;
        if(iblood>ublood){
            this.win(2);
        }else if(iblood==ublood){
            this.win(1);
        }else{
            this.win(0);
        }
    },
    win(flag){
        cc.log("比赛结果");

        this.timeStop();
        this.touchFlag = false;

        this.alert.active = true;
        var str = "红方胜利";
        if(flag==1){
            str = "平局";
        }else if(flag==0){
            str = "蓝方胜利";
        }
        this.alert.getChildByName('msg').getComponent(cc.Label).string = str;
    },
    timeStart(){
        cc.log("计时开始");

        var that = this;
        this.schedule(function(){
            if(that.time>0){
                that.timeLabel.node.getComponent(cc.Label).string = --that.time;
            }else{
                that.winOrLose();
            }
        },1);
    },
    timeStop(){
        cc.log("计时结束");

        this.unscheduleAllCallbacks();
    },
    onMessage(event){
        cc.log("接收消息:" + event.data);

        var self = this.parent;

        var message = JSON.parse(event.data);
        
        if(message.type==200100){
            self.responseWind(message.content);
        }else if(message.type==200081){
            self.u.getComponent('u').responseAttack(message.content);
        }
    },
    responseWind(content){
        cc.log("改变风向");

        this.wind = content.wind;
        var windStr = this.wind;
            if(this.wind>0){
                this.windLabel.node.color = cc.Color.RED;
                windStr += '→';
            }else if(this.wind<0){
                this.windLabel.node.color = cc.Color.BLUE;
                windStr = '←'+windStr;
            }else{
                this.windLabel.node.color = cc.Color.WHITE;
            }
            this.windLabel.getComponent(cc.Label).string = windStr;
    },
    onExitClick(){
        cc.director.loadScene('start');
    },
    update (dt) {

    },
});
